package com.gomyb.admin.core.storage;

import com.gomyb.storage.model.CloudStorageAuth;
import com.gomyb.storage.model.StorageSource;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/3/6 20:13
 */
@Setter
@Getter
@Configuration
@Component
@ConfigurationProperties(prefix = "storage")
public class StorageConfiguration {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private StorageSource source;

    public void setSource(String source) {
        if (StringUtils.isNotEmpty(source)) {
            StorageSource[] values = StorageSource.values();
            for (StorageSource value : values) {
                if (value.getCode().equals(source)) {
                    this.source = value;
                    return;
                }
            }
            this.source = StorageSource.OSS;
        }
    }

    @Bean
    public CloudStorageAuth cloudStorageAuth() {
        CloudStorageAuth auth = new CloudStorageAuth();
        auth.setAccessKeyId(accessKeyId);
        auth.setAccessKeySecret(accessKeySecret);
        auth.setBucketName(bucketName);
        auth.setEndpoint(endpoint);
        auth.setSource(StorageSource.OSS);
        return auth;
    }

}
